package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    /**
     * 分页查询课程信息列表
     * 请求
     * Post:/list
     * 参数
     *      公共的分页参数
     *       查询条件 Json
     *  返回值
     *      PageResponseResult
     */
    @PostMapping("/list")
    public ResponseResult list(PageRequest request, @RequestBody CourseDto dto){
        return courseBaseService.courseList(request,dto);
    }

    //上传课程封面图片
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile uploadFile) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        return courseBaseService.upload(uploadFile);
    }

    //保存课程
    @PostMapping
    public ResponseResult saveCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.saveCourseBase(dto);
    }

    //根据课程id查询课程
    @GetMapping("{courseBaseId}")
    public ResponseResult findById(@PathVariable("courseBaseId") Long courseBaseId){
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        //转换为vo对象
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(vo);
    }

    //修改课程
    @PutMapping
    public ResponseResult updateCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.updateCourseBase(dto);
    }
    //删除课程信息
    @DeleteMapping("/{courseId}")
    public ResponseResult deleteCourseBase(@PathVariable Long courseId){
        return courseBaseService.deleteCourseBase(courseId);
    }

    /**
     * 提交审核
     *          GET  /commit/{courseBaseId}
     */
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId){
        return courseBaseService.commit(courseBaseId);
    }
    /**
     * 课程审核
     * POST /approve
     */
    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto){
        return courseBaseService.approve(dto);
    }
}
